package com.ftg.learn.student.mapper;

import com.ftg.learn.student.entity.Menu;
import com.ftg.learn.student.entity.Student;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

@Mapper
public interface StudentMapper {

    /**
     * sql   method
     */
    @Select("select * from student")
    List<Student> showAllStudent();


    @Select("select * from student where sno = #{id}")
    List<Student> findid(@Param("id") String id);


    @Select("<script>"
            + "select * from student "
            + "  <where>"
            + "      <if test='id != null'>sno = #{id}</if>"
            + "  </where>"
            + "</script>")
    List<Student> findid1(@Param("id") String id);

    /**
    sql构建器 java - 生成wsql
     */
    @SelectProvider(type= StudentProvoder.class ,method = "createFindId2")
    List<Student> findid2(@Param("id") String id);

    @Insert("insert into student(sno,sname,sage,ssex) values(#{sno},#{sname},#{sage},#{ssex})")
    int save(@Param("sno") String sno,
             @Param("sname")String sname,
             @Param("sage")Integer sage,
             @Param("ssex")String ssex);

    /**
     * 为了添加时，以主键是否存在做判断
     * @param id 主键s
     * @return
     */
    @Select("select * from student where sno = #{id}")
    Student getOneBySno(@Param("id") String id);


    @Insert("insert into student(sno,sname,sage,ssex) values(#{sno},#{sname},#{sage},#{ssex})")
    int savePojo(Student stu);

    @Select("select * from student")
    @Results({
        @Result(column = "sno",property ="scs" ,many = @Many(select="com.ftg.learn.student.mapper.ScMapper.findOne"))
    })
    List<Student> showAllWithSc();


    @Select("select id,name,pid,url from menu where pid = #{pid}")
    @Results({
            @Result(column = "id",property = "list" ,many = @Many(select = "findMenuWith1"))
    })
    List<Menu> findMenuWith(@Param("pid") int pid);

    @Select("select id,name,pid,url from menu where pid = #{pid}")
    List<Menu> findMenuWith1(@Param("pid") int pid);

//    @Select("select id,name,pid,url from menu where id = 1")
    @Select("select id,name,pid,url from menu where id = 1")
    @Results({
            @Result(column = "id",property = "list" ,many = @Many(select = "findMenuWith"))
    })
    List<Menu> allMenu();

}
